const EventEmitter = require('events')
const debug = require('debug')
const inquirer = require('inquirer')
const clearConsole = require('clear')
const { logWithSpinner, stopSpinner }  = require('../utils')
module.exports = class Initial extends EventEmitter {
  constructor(name, context) {
    super()
    this.name = name
    this.context = process.env.VUE_CLI_CONTEXT = context
    const { presetPrompt, featurePrompt } = this.resolveIntroPrompts()
    this.presetPrompt = presetPrompt
    this.featurePrompt = featurePrompt
    this.injectedPrompts = []
    this.promptCompleteCbs = []
    this.run = this.run.bind(this)
  }

  async init(cliOptions = {}, preset = null) {
    const { name, context } = this
    if (!preset) {
      preset = await this.promptAndResolvePreset()
    }
  }

  run(command, args) {
    if (!args) {
      ;[command, ...args] = command.split(/\s+/)
    }
    return execa(command, args, { cwd: this.context })
  }

  async promptAndResolvePreset (answers = null) {
    // prompt
    if (!answers) {
      clearConsole()
      answers = await inquirer.prompt(this.resolveFinalPrompts())
    }
    debug('vue-cli:answers')(answers)

    if (answers.packageManager) {
      saveOptions({
        packageManager: answers.packageManager
      })
    }

    let preset
    if (answers.preset && answers.preset !== '__manual__') {
      preset = await this.resolvePreset(answers.preset)
    } else {
      // manual
      preset = {
        useConfigFiles: answers.useConfigFiles === 'files',
        plugins: {}
      }
      answers.features = answers.features || []
      // run cb registered by prompt modules to finalize the preset
      this.promptCompleteCbs.forEach(cb => cb(answers, preset))
    }

    // validate
    // validatePreset(preset)

    // save preset
    // if (answers.save && answers.saveName) {
    //   savePreset(answers.saveName, preset)
    // }

    debug('vue-cli:preset')(preset)
    return preset
  }

  getPresets() {
    const savedOptions = loadOptions()
    return Object.assign({}, savedOptions.presets, defaults.presets)
  }
  resolveIntroPrompts() {
    const presetPrompt = {
      name: 'preset',
      type: 'list',
      message: `Please pick a preset:`,
      choices: [
        {
          name: 'Admin Template(后台模板)',
          value: 'admin'
        },
        {
          name: 'Front Template(前端模板)',
          value: 'front'
        }
      ]
    }
    const featurePrompt = {
      name: 'features',
      when: 'front',
      type: 'checkbox',
      message: 'Check the features needed for your project:',
      choices: [],
      pageSize: 10
    }
    return {
      presetPrompt,
      featurePrompt
    }
  }
  resolveFinalPrompts () {
    // patch generator-injected prompts to only show in manual mode
    // this.injectedPrompts.forEach(prompt => {
    //   const originalWhen = prompt.when || (() => true)
    //   prompt.when = answers => {
    //     return isManualMode(answers) && originalWhen(answers)
    //   }
    // })
    const prompts = [
      this.presetPrompt
    //   this.featurePrompt
    //   ...this.injectedPrompts,
    //   ...this.outroPrompts
    ]
    debug('vue-cli:prompts')(prompts)
    return prompts
  }
}
